<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end">
          <el-button
            type="primary"
            @click="addPermFn('one')"
          >添加权限</el-button>
        </el-row>
        <el-divider />
        <el-table :data="permList" border row-key="id" style="width: 100%">
          <el-table-column prop="title" label="权限名称" width="180" />
          <el-table-column prop="code" label="权限标识" />
          <el-table-column prop="description" label="权限描述" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.pid === '0'"
                type="primary"
                size="mini"
                @click="addPermFn(scope.row.id)"
              >添加子权限</el-button>
              <el-button
                size="mini"
                type="success"
                @click="editPermFn(scope.row.id)"
              >编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="closePerm(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center">
          <el-pagination
            layout="prev, pager, next"
            :page-size="size"
            :current-page="1"
            :total="total"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
    </div>
    <el-dialog
      v-if="dialogVisible"
      :title="titledialog"
      :visible="dialogVisible"
      @close="close"
    >
      <el-form ref="form" :model="PermData" :rules="rules" label-width="80px">
        <el-form-item label="权限名称" prop="title">
          <el-input v-model="PermData.title" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="PermData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="PermData.description" />
        </el-form-item>
        <el-row class="btn" type="flex" justify="end">
          <el-button type="primary" @click="addFn">确认</el-button>
          <el-button @click="close">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermList,
  getPermTotal,
  addPerm,
  delPerm,
  getAssignPerm,
  editPerm
} from '@/api/permissions'
import { listToTree } from '@/utils/index'
export default {
  data() {
    return {
      permList: [], // 权限列表
      size: 90,
      page: 1,
      total: 0,
      dialogVisible: false,
      titledialog: '添加权限', // 弹框标题
      // 权限数据
      PermData: {
        title: '',
        code: '',
        description: '',
        pid: '0',
        type: '1'
      },
      // 校验规则
      rules: {
        title: [
          {
            required: true,
            message: '请输入权限名称',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入权限标识',
            trigger: 'blur'
          }
        ],
        description: [
          {
            required: true,
            message: '请输入权限描述',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getpermlist()
  },
  methods: {
    // 获取权限列表
    async getpermlist() {
      const start = (this.page - 1) * this.size
      this.permList = await getPermList({
        _limit: this.size,
        _start: start
      })
      this.permList = listToTree(this.permList, '0')
      //   // 获取权限数量
      this.total = await getPermTotal()
    },
    currentChange(page) {
      this.page = page
      this.getpermlist()
    },
    // 打开新增权限窗口
    addPermFn(pid) {
      this.dialogVisible = true
      if (pid !== 'one') {
        this.titledialog = '添加子权限'
        this.PermData.type = '2'
        this.PermData.pid = String(pid)
      } else {
        this.titledialog = '添加权限'
      }
    },
    // 确认添加权限
    async addFn() {
      await this.$refs.form.validate()
      // 有id就是编辑权限,没有就是新增权限和子权限
      if (this.PermData.id) {
        await editPerm(this.PermData)
        this.$message.success('编辑权限成功')
      } else {
        await addPerm(this.PermData)
        this.$message.success('添加权限成功')
      }
      this.close()
      this.getpermlist()
    },
    // 关闭弹框
    close() {
      this.dialogVisible = false
      this.PermData = {
        title: '',
        code: '',
        description: '',
        pid: '0',
        type: '1'
      }
    },
    // 删除权限
    closePerm(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delPerm(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getpermlist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 打开编辑弹框并让数据回显
    async editPermFn(id) {
      this.dialogVisible = true
      this.titledialog = '编辑权限'
      this.PermData = await getAssignPerm(id)
    }
  }
}
</script>

<style>
</style>
